var prefix = "/finance/balance";
$(function () {
    load();

    $("#upload").click(function () {
        var file = $('#excelFile').val();
        if (!file) {
            layer.msg("请选择导入文件！");
            return;
        }

        layer.confirm('确认要批量充值吗？', {
            btn: ['确定', '取消']
        }, function () {
        	$(".layui-layer-btn0").hide();
         	//防止重复点击
	    	layer.open({type:1,title:false,closeBtn:false}) 
            var formdata = new FormData(document.getElementById("batchRecharge"));
            $.ajax({
                url: "/finance/balance/batchRecharge",
                type: "post",
                data: formdata,
                contentType: false, //- 必须false才会自动加上正确的Content-Type
                processData: false, //- 必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理
                success: function (r) {
                    layer.msg(r.msg);
                    reLoad();
                    //关闭所有弹窗
                    layer.closeAll();
                },
                error: function () {
                    layer.msg("批量充值失败！请与管理员联系！");
                    reLoad();
                }
            });
        });
    });
});
$('#balanceTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#balanceTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    getTotalBalance();
    $('#balanceTable').bootstrapTable(
        {
            method: 'get', // 服务器数据的请求方式 get or post
            url: prefix + "/list", // 服务器数据的加载地址
            showRefresh: true,
            // showToggle : true,
            iconSize: 'outline',
            // toolbar: '#exampleToolbar',
            striped: true, // 设置为true会有隔行变色效果
            dataType: "json", // 服务器返回的数据类型
            pagination: true, // 设置为true会在底部显示分页条
            pageList: [10, 20, 50],
            // queryParamsType : "limit",
            // //设置为limit则会发送符合RESTFull格式的参数
            singleSelect: false, // 设置为true将禁止多选
            // contentType : "application/x-www-form-urlencoded",
            // //发送到服务器的数据编码类型
            pageSize: 10, // 如果设置了分页，每页数据条数
            pageNumber: 1, // 如果设置了分布，首页页码
            // search : true, // 是否显示搜索框
            showColumns: true, // 是否显示内容下拉框（选择显示的列）
            sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
            // "server"
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: params.offset,
                    order: 'desc',
                    queryItem: $.trim($("#queryItem").val()),
                    centreId: $("*[name='centreId']").val(),
                    role: $('#role').val(),
                    start: $('#start').val(),
                    end: $('#end').val()
                };
            },
            columns: [
                {
                    title: '序号',// 列标题
                    formatter: function (value, row, index) {
                        var pageSize = $('#balanceTable').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                        var pageNumber = $('#balanceTable').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    field: 'mid', // 列字段名
                    title: '会员ID' // 列标题
                },
                {
                    field: 'nickname',
                    title: '昵称'
                },
                {
                    field: 'realname',
                    title: '姓名'
                },
                {
                    field: 'mobile',
                    title: '手机号码'
                },
                {
                    field: 'isteam',
                    title: '等级',
                    formatter: function (value, row, index) {
                        if (value === 3) {
                            return '门店'
                        } else {
                            return '会员'
                        }
                    }
                },
                {
                    field: 'balance',
                    title: '余额',
                    formatter: function (value, row, index) {
                        return '&yen;' + value
                    }
                },
                {
                    title: '操作',
                    field: 'mid',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var r = '<a class="btn btn-primary btn-sm ' + s_recharge_h + '" href="#" mce_href="#" title="充值" onclick="recharge(\''
                            + value + '\')"><i class="fa fa-jpy"></i>充值</a> ';

                        var d = '<a class="btn btn-warning btn-sm ' + s_recharge_h + '" href="#" mce_href="#" title="扣款" onclick="deduct(\''
                            + value + '\')"><i class="fa fa-warning"></i>扣款</a> ';

                        var s = '<a class="btn btn-success btn-sm ' + s_cash_h + '" href="#" mce_href="#" title="提现" onclick="cash(\''
                            + value + '\')"><i class="fa fa-money"></i>提现</a> ';

                        var x = '<a class="btn btn-info btn-sm ' + s_payin_h + '" href="#" mce_href="#" title="收支" onclick="payin(\''
                            + value + '\')"><i class="fa fa-list"></i>收支</a> ';

                        return r + d + s + x;
                    }
                }]
        });
}

function reLoad() {
    $('#balanceTable').bootstrapTable('refresh');
    getTotalBalance();
}

function remove(supplyId) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'supplyId': supplyId
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

/**
 * 余额总计
 */
function getTotalBalance() {
    $.ajax({
        type: 'POST',
        data: {
            queryItem: $.trim($("#queryItem").val()),
            centreId: $("*[name='centreId']").val(),
            role: $('#role').val(),
            start: $('#start').val(),
            end: $('#end').val()
        },
        url: prefix + '/getTotalBalance',
        success: function (r) {
            if (r.code == 0) {
                var total = r.totalBalance;
                $('#totalInfo').text("合计金额:￥" + total);
            }
        }
    });
}

/**
 * 充值
 * @param mid
 */
function recharge(mid) {
    // iframe层
    layer.open({
        id: 'balance_recharge',
        type: 2,
        title: '充值',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/recharge/' + mid
    });
}

/**
 * 扣款
 * @param mid
 */
function deduct(mid) {
    layer.open({
        id: 'balance_deduct',
        type: 2,
        title: '扣款',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/deduct/' + mid
    });
}

function payin(mid) {
    layer.open({
        id: 'balance_detail',
        type: 2,
        title: '收支明细',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/payin/' + mid
    });
}

/**
 * 提现
 * @param mid
 */
function cash(mid) {
    layer.open({
        id: 'balance_cash',
        type: 2,
        title: '提现',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/cash/' + mid
    });
}

/**
 * 余额记录导出
 */
function exportBalances() {
    var queryItem = $.trim($("#queryItem").val());
    var centreId = $("*[name='centreId']").val();
    var role = $('#role').val();
    var start = $('#start').val();
    var end = $('#end').val();
    window.location.href = prefix + "/export?centreId=" + centreId + "&queryItem=" + queryItem + "&start=" + start + "&end=" + end + "&role=" + role;
}